//
//  ViewController.m
//  CoreDataDemo
//
//  Created by LZXuan on 15-6-1.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "ViewController.h"
#import "UserModel.h"
#import "CoreData+MagicalRecord.h"


@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_dataArr;
}
@property (nonatomic,strong) NSMutableArray *dataArr;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property (weak, nonatomic) IBOutlet UIButton *deletButton;
@property (weak, nonatomic) IBOutlet UIButton *updateButton;
@property (weak, nonatomic) IBOutlet UIButton *fetchButton;
@property (weak, nonatomic) IBOutlet UIButton *fetchAllButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)addClick:(UIButton *)sender;
- (IBAction)deleteClick:(id)sender;
- (IBAction)updateClick:(id)sender;
- (IBAction)fetchClick:(id)sender;
- (IBAction)fetchAllClick:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化表格
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.dataArr = [[NSMutableArray alloc] init];
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
   
}
#pragma mark - UITableView协议
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    UserModel *model = self.dataArr[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"name:%@ age:%ld",model.name,model.age.integerValue];
    
    return cell;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.nameTextField resignFirstResponder];
    [self.ageTextField resignFirstResponder];
}

#pragma mark - 第三方库 MagicalRecord操作数据库


//操作 数据库sqlite  不需要 sql 语句
- (IBAction)addClick:(UIButton *)sender {
   //增加
    //向数据库增加一个模型对象
    UserModel *model = [UserModel MR_createEntity];
    model.name = self.nameTextField.text;
    model.fName = [NSString stringWithFormat:@"%C",[self.nameTextField.text characterAtIndex:0]];
    model.age = @(self.ageTextField.text.integerValue);
    
    //增加之后要保存 到本地
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

- (IBAction)deleteClick:(id)sender {
    //先找到
    //根据名字找
    NSArray *arr = [UserModel MR_findByAttribute:@"name" withValue:self.nameTextField.text];
    for (UserModel *model in arr) {
        //把找到的都删除
        [model MR_deleteEntity];
    }
    //要保存
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}
//修改
- (IBAction)updateClick:(id)sender {
    //先找到
    //根据名字找
    NSArray *arr = [UserModel MR_findByAttribute:@"name" withValue:self.nameTextField.text];
    for (UserModel *model in arr) {
        model.age = @(self.ageTextField.text.integerValue);//找到之后修改
    }
    //要保存
    [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
}

//查询
- (IBAction)fetchClick:(id)sender {
    //根据名字找
    //根据数据模型的属性名 对应的值 找到相应地数据模型
    NSArray *arr = [UserModel MR_findByAttribute:@"name" withValue:self.nameTextField.text];
    
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:arr];
    [self.tableView reloadData];
    
    //[UserModel MR_findFirst];//查找第一个数据模型对象
    
}

//查询所有
- (IBAction)fetchAllClick:(id)sender {
    //查找所有
    NSArray *arr = [UserModel MR_findAll];
    
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:arr];
    [self.tableView reloadData];
}
@end





